//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;
short create_count = 0;
short last_spawn;
short last_abil;
short summon_mess = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Shaper Rawal");
	set_level(ME,34);
	change_max_health(ME,400);
	set_boss_level(ME,2);
	set_new_abil(ME,20);
	last_spawn = get_current_tick();
	last_abil = get_current_tick();

	set_resistance(ME,7,90);
	
	if (gf(100,23) > 0)
		erase_char(ME);
	break;

beginstate DEAD_STATE;
	sf(101,2,1);
		toggle_quest(5,4);
		toggle_quest(32,4);
		toggle_quest(33,4);
	
	begin_talk_mode(147);
break;

beginstate START_STATE; 
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

		
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking

	if (target_ok() == FALSE)
		set_state(START_STATE);
		
	if ((gf(3,20) == 0) && (dist_to_pc() <= 8)) {
		begin_talk_mode(145);
		end();
		}
	if (gf(3,20) > 0) {
		if ((tick_difference(last_spawn,get_current_tick()) > 0) && (get_attitude(ME) >= 10) && (create_count < 8) &&
		  (get_nearest_party_char(8) >= 0)) {
			print_named_str(ME,"calls forth a pair of creations!");

			spawn_creature(32 + create_count);
			set_summon_level(40 + create_count,1);
			place_particle_num(40 + create_count,10,7,10);
			
			if (gf(2,13) != 1)
				set_boss_level(40 + create_count,1);

			spawn_creature(32 + create_count + 1);
			set_summon_level(40 + create_count + 1,1);
			place_particle_num(40 + create_count + 1,10,7,10);
			
			if (gf(2,13) != 1)
				set_boss_level(40 + create_count + 1,1);
			if (gf(2,13) == 2) {
				print_str_color("You are able to take control of one of them!",2);
				set_attitude(40 + create_count + 1,3);
				set_new_abil(40 + create_count + 1,20);
				set_attack_bonus(40 + create_count + 1,8);
				}
			
					
			run_char_animation(2,1,50);	
			pc_heard_sound_delay(136,100);						
			
			last_spawn = get_current_tick();
			create_count = create_count + 2;
			
			if (summon_mess == 0) {
				summon_mess = 1;
				begin_talk_mode(146);
				}
				
			end();
			}
		

		if ((tick_difference(last_abil,get_current_tick()) > 1) && (get_attitude(ME) >= 10)) {
			run_char_animation(2,1,50);	
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"surrounds himself with an aura of power.");
			pc_heard_sound_delay(179,250);						
	  		create_missile_spiral(147,40,6,2);
	  		damage_nearby(get_ran(1,200,300),6,1,0);
			status_nearby(-10,6,1,0);
			status_nearby(6,6,20,0);

			last_abil = get_current_tick();
			end();
			
			}

		}

		
	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
	if (gf(3,19) > 0) {
		print_str_color("Talking: Shaper Rawal refuses to speak with you. Fortunately, ",2);
		print_str_color("  with effort, he keeps from killing you.",2);
		}
		else begin_talk_mode(70);
break;